// app.js
App({
  baseURL: 'https://api.it120.cc/conner',
  request(url, params = {}){
    return new Promise((resolve, reject) => {
      wx.showLoading({
        title: '加载中...'
      })
      wx.request({
        url: this.baseURL + url, 
        method: params.method || 'GET',
        data: params.data || '',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          token: wx.getStorageSync('token')||''
        },
        success: res => {
          // 判断token是否过期
          /* 
          const errCode = [401, 403],
          if(errCode.includes(res.data.code)){
            wx.showToast({
              title:'登录状态过期、获取未登录',
              icon: 'error',
              success: res => {
                // 清空各种缓存
                wx.removeStorageSync('token');
                wx.removeStorageSync('userInfo');
                wx.removeStorageSync('openid');
                 wx.removeStorageSync('uid');
                //  跳转到登录页
                wx.navigateTo({
                  url: '/pages/login/login'
                })
              }
            })
          }
          */
          if(res.data.code === 2000){
            wx.showToast({
              title:'登录状态过期',
              icon: 'error',
              success: res => {
                // 清空各种缓存
                wx.removeStorageSync('token');
                wx.removeStorageSync('userInfo');
                wx.removeStorageSync('openid');
                 wx.removeStorageSync('uid');
                //  跳转到登录页
                wx.navigateTo({
                  url: '/pages/login/login'
                })
              }
            })
          }
          resolve(res)
        },
        fail: err=>{
          reject(err)
        },
        complete: () => {
          wx.hideLoading()
        }
      })
    })
  },
  isLogin(){
    return !!wx.getStorageSync('token');
  },
  globalData: {
    userInfo: null
  }
})
